package com.yan.controller;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @Author: 汪焰
 * @Date: 2021/7/8 22:07
 */
@RestController
public class DelayedController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 访问地址5秒后 监听器收到消息
     *  http://localhost:8080/meeting/5
     *
     * @Author: 汪焰
     * @Date: 2021/7/8 23:37
     */
    @GetMapping("/meeting/{second}")
    public String bookMeeting(@PathVariable Integer second) {

        MessageProperties msgPro = MessagePropertiesBuilder.newInstance()
                .setHeader("x-delay", (second - 1) * 1000)
                .setContentEncoding("utf-8")
                .build();

        Message msg = MessageBuilder.withBody("还有10s开始开会了".getBytes())
                .andProperties(msgPro)
                .build();
        amqpTemplate.send("exchange.yan.delayed", "key.yan.delayed", msg);
        return "会议室订好了";
    }


}
